<?PHP
/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2010 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/
/**
 * THIS CLASS IS FOR DEVELOPERS TO MAKE CUSTOMIZATIONS IN
 */
require_once('modules/edanz_Beneficiaries/edanz_Beneficiaries_sugar.php');
class edanz_Beneficiaries extends edanz_Beneficiaries_sugar {
	
	function edanz_Beneficiaries(){	
		parent::edanz_Beneficiaries_sugar();
	}

	function save_relationship_changes($is_update, $exclude=array())
	{
		$new_rel_id = false;
		$new_rel_link = false;
		//this allows us to dynamically relate modules without adding it to the relationship_fields array
		if(!empty($_REQUEST['relate_id']) && !in_array($_REQUEST['relate_to'], $exclude) && $_REQUEST['relate_id'] != $this->id){
			$new_rel_id = $_REQUEST['relate_id'];
			$new_rel_relname = $_REQUEST['relate_to'];
            if(!empty($this->in_workflow) && !empty($this->not_use_rel_in_req)) {
                $new_rel_id = $this->new_rel_id;
                $new_rel_relname = $this->new_rel_relname;
            }
			$new_rel_link = $new_rel_relname;
			//Try to find the link in this bean based on the relationship
			foreach ( $this->field_defs as $key => $def ) {
				if (isset($def['type']) && $def['type'] == 'link' 
				&& isset($def['relationship']) && $def['relationship'] == $new_rel_relname) {
					$new_rel_link = $key;
				}
			}
$GLOBALS['log']->debug('primary ben: '.$_REQUEST['primary_beneficiary']);
$GLOBALS['log']->debug('new_rel_link: '.$new_rel_link);
//$GLOBALS['log']->debug('new_rel_join table : '.$this->$new_rel_link->_relationship->join_table);
		}

		if(isset($_REQUEST['primary_beneficiary']) && $_REQUEST['primary_beneficiary'] == '1'){
			$date_modified = gmdate($GLOBALS['timedate']->get_db_date_time_format());
			$query='UPDATE editors_beneficiaries SET primary_beneficiary = 0 , date_modified = \''.$date_modified.'\' WHERE editor_id = \''.$new_rel_id.'\' AND deleted=0 AND primary_beneficiary = 1';
			$this->db->query($query, true,"Error unsetting primary record: ");
//			$where = ' WHERE editor_id = \''.$new_rel_id.'\' AND deleted=0';
//			$this->load_relationship($new_rel_link);
//			$this->$new_rel_link->_update_row(array('primary_beneficiary' => '0'), 'editors_beneficiaries', $where);
		}

$GLOBALS['log']->debug('this: '.print_r($this, true));
		if(!empty($new_rel_id)){

			if($this->load_relationship($new_rel_link)){
$GLOBALS['log']->fatal("a");
				$this->$new_rel_link->add($new_rel_id, array('primary_beneficiary' => $_REQUEST['primary_beneficiary']));

			}else{
				$lower_link = strtolower($new_rel_link);
				if($this->load_relationship($lower_link)){
$GLOBALS['log']->fatal("b");
					$this->$lower_link->add($new_rel_id);

				}else{
					require_once('data/Link.php');
					$rel = Relationship::retrieve_by_modules($new_rel_link, $this->module_dir, $GLOBALS['db'], 'many-to-many');

					if(!empty($rel)){
						foreach($this->field_defs as $field=>$def){
							if($def['type'] == 'link' && !empty($def['relationship']) && $def['relationship'] == $rel){
								$this->load_relationship($field);
$GLOBALS['log']->fatal("c");
								$this->$field->add($new_rel_id);
								return;

							}

						}
                        //ok so we didn't find it in the field defs let's save it anyway if we have the relationshp

						$this->$rel=new Link($rel, $this, array());
						$this->$rel->add($new_rel_id);
					}
				}

			}

		}

	}


	
}
?>
